from datetime import datetime
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Player, Game

# Create your views here.
def index(request):
    return HttpResponse("Hello world.")

def get_player(request, player_id):
    player = get_object_or_404(Player, steamid=player_id)
    return HttpResponse("\t".join((str(player.score), str(player.win), str(player.lose), str(player.abbadon))))

def get_players(request):
    queries = request.GET
    playerIds = []
    for i in range(10):
        playerIds.append(request.GET.get("p" + str(i)))
    print(playerIds)
    outputScores = []
    for i in range(10):
        try:
            if playerIds[i]:
                player = Player.objects.get(steamid = playerIds[i])
                outputScores.append(str(player.score))
            else:
                outputScores.append("-1")
        except Player.DoesNotExist:
            outputScores.append("1000")
    return HttpResponse("\t".join(outputScores))

def register_game(request, game_id):
    if Game.objects.filter(gameid = game_id).count() > 0:
        return HttpResponse("game exists already.") 
    game = Game(gameid = game_id, createdate = datetime.now()) 
    game.save()
    return HttpResponse("game saved.")
